home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 June / EnigmA AMIGA RUN 08 (1996)(G.R. Edizioni)(IT)[!][issue 1996-06][EARSAN CD VII].iso / earcd / c-lang / vbcc.lha / vbcc / declaration.c < prev    next >
C/C++ Source or Header  |  1996-05-15  |  52KB  |  1,354 lines

  1. /*  $VER: vbcc (declaration.c) V0.3     */
  2.  
  3. #include "vbc.h"
  4.  
  5. #define PARAMETER 8
  6. #define OLDSTYLE 16
  7.  
  8. struct const_list *initialization(struct Typ *,int);
  9. int test_assignment(struct Typ *,np);
  10. int return_sc,has_return;
  11.  
  12. extern int float_used;
  13. extern void optimize(long,struct Var *);
  14.  
  15. int settyp(int typnew, int typold)
  16. /* Unterroutine fuer declaration_specifiers()               */
  17. {
  18.     static int warned_long_double;
  19.     if(DEBUG&2) printf("settyp: new=%d old=%d\n",typnew,typold);
  20.     if(typold==LONG&&typnew==FLOAT){ error(203); return(DOUBLE);}
  21.     if(typold==LONG&&typnew==DOUBLE){
  22.         if(!warned_long_double){error(204);warned_long_double=1;}
  23.         return(DOUBLE);
  24.     }
  25.     if(typold!=0&&typnew!=INT){error(47);return(typnew);}
  26.     if(typold==0&&typnew==INT) return(INT);
  27.     if(typold==0) return(typnew);
  28.     if(typold==SHORT||typold==LONG) return(typold);
  29.     error(48);
  30.     return(typnew);
  31. }
  32.  
  33. #define dsc if(storage_class) error(49); if(typ||type_qualifiers) error(50)
  34.  
  35. struct Typ *declaration_specifiers(void)
  36. /* Erzeugt neuen Typ und gibt Zeiger darauf zurueck,      */
  37. /* parst z.B. unsigned int, struct bla etc.               */
  38. /* evtl. muessen noch storage-classes uns strengere       */
  39. /* Pruefungen etc. eingebaut werden                       */
  40. {
  41.     int typ=0,type_qualifiers=0,notdone,storage_class;
  42.     char *merk,*imerk,sident[MAXI],sbuff[MAXI];
  43.     struct Typ *new=(struct Typ *)mymalloc(TYPS),*t,*ts;
  44.     struct struct_declaration *ssd,*p;
  45.     struct struct_identifier *si;
  46.     struct Var *v;
  47.     storage_class=0;
  48.     new->next=0; new->exact=0;
  49.     do{
  50.         merk=s;killsp();cpbez(buff);notdone=0;
  51.         if(DEBUG&2) printf("ts: %s\n",buff);
  52.         if(!strcmp("struct",buff)) notdone=STRUCT;
  53.         if(!strcmp("union",buff)) notdone=UNION;
  54.         if(notdone!=0){
  55.             killsp();
  56.             if(*s!='{'){
  57.                 cpbez(sident);
  58.                 p=find_struct(sident);
  59.                 if(p==0) {typ=notdone|UNCOMPLETE;
  60.                           new->exact=(void *)add_identifier(sident,strlen(sident));
  61.                 }else{
  62.                     new->exact=p;
  63.                     typ=new->flags=notdone;
  64.                 }
  65.                 killsp();
  66.                 if(*s==';') error(205);
  67.             }else *sident=0;
  68.             if(*s=='{'){
  69.                 si=first_si[nesting];
  70.                 while(si){
  71.                     if(!strcmp(si->identifier,sident)) {error(13,sident);break;}
  72.                     si=si->next;
  73.                 }
  74.                 s++;
  75.                 killsp();
  76.                 ssd=(struct struct_declaration *)mymalloc(MAXSTRUCTSIZE);
  77.                 ssd->count=0;
  78.                 imerk=ident;
  79.                 ts=declaration_specifiers();
  80.                 while(*s!='}'&&ts){
  81.                     ident=sbuff;
  82.                     t=declarator(clone_typ(ts));
  83.                     killsp();
  84.                     if(*s==':'){
  85.                     /*  bitfields werden hier noch ignoriert    */
  86.                         np tree;
  87.                         if((ts->flags&15)!=INT) error(51);
  88.                         s++;killsp();tree=assignment_expression();
  89.                         if(type_expression(tree)){
  90.                             if(tree->flags!=CEXPR) error(52);
  91.                             if((tree->ntyp->flags&15)<CHAR||(tree->ntyp->flags&15)>LONG) error(52);
  92.                         }
  93.                         if(tree) free_expression(tree);
  94.                     }else{
  95.                         if(*ident==0) error(53);
  96.                     }
  97.                     if(type_uncomplete(t)){
  98.                         error(14,sbuff);
  99.                         freetyp(t);
  100.                         break;
  101.                     }
  102.                     if((t->flags&15)==FUNKT)
  103.                         error(15,sbuff);
  104.  
  105.                     if(*ident!=0){
  106.                         int i=ssd->count;
  107.                         while(--i>=0)
  108.                             if(!strcmp(ssd->sl[i].identifier,ident))
  109.                                 error(16,ident);
  110.                     }
  111.                     ssd->sl[ssd->count].styp=t;
  112.                     ssd->sl[ssd->count].identifier=add_identifier(ident,strlen(ident));
  113.                     ssd->count++;
  114.                     killsp();
  115.                     if(*s==',') {s++;killsp();continue;}
  116.                     if(*s!=';') error(54); else s++;
  117.                     killsp();
  118.                     if(*s!='}'){
  119.                         if(ts) freetyp(ts);
  120.                         ts=declaration_specifiers();killsp();
  121.                     }
  122.                 }
  123.                 if(ts) freetyp(ts);
  124.                 if(ssd->count==0) error(55);
  125.                 ident=imerk;
  126.                 new->exact=add_sd(ssd);
  127.                 typ=notdone;
  128.                 free(ssd);
  129.                 if(*s!='}') error(56); else s++;
  130.                 if(*sident!=0) add_struct_identifier(sident,new->exact);
  131.                 new->flags=notdone|type_qualifiers;
  132.             }
  133.             notdone=1;
  134.         }
  135.         if(!strcmp("enum",buff)){
  136.         /*  enumerations; die Namen werden leider noch ignoriert    */
  137.             killsp();notdone=1;
  138.             if(*s!='{'){cpbez(buff);killsp();}
  139.             if(*s=='{'){
  140.                 zlong val; struct Var *v; struct Typ *t;
  141.                 val=l2zl(0L);
  142.                 s++;killsp();
  143.                 while(*s!='}'){
  144.                     cpbez(sident);killsp();
  145.                     t=(struct Typ *)mymalloc(TYPS);
  146.                     t->flags=CONST|INT;
  147.                     t->next=0;
  148.                     if(find_var(sident,nesting)) error(17,sident);
  149.                     v=add_var(sident,t,AUTO,0); /*  AUTO hier klug? */
  150.                     if(*s=='='){
  151.                         s++;killsp();
  152.                         v->clist=initialization(v->vtyp,0);
  153.                         val=zi2zl(v->clist->val.vint);killsp();
  154.                     }else{
  155.                         v->clist=(struct const_list *)mymalloc(CLS);
  156.                         v->clist->val.vint=val;
  157.                         v->clist->next=v->clist->other=0;
  158.                         v->clist->tree=0;
  159.                     }
  160.                     vlong=l2zl(1L);val=zladd(val,vlong);
  161.                     v->vtyp->flags=CONST|ENUM;
  162.                     if(*s=='}') break;
  163.                     if(*s==',') s++; else error(57);
  164.                     killsp();
  165.                 }
  166.                 s++;
  167.             }
  168.             killsp();
  169.             typ=INT;*buff=0;
  170.         }
  171.         if(!strcmp("void",buff)) {typ=settyp(VOID,typ);notdone=1;}
  172.         if(!strcmp("char",buff)) {typ=settyp(CHAR,typ);notdone=1;}
  173.         if(!strcmp("short",buff)) {typ=settyp(SHORT,typ);notdone=1;}
  174.         if(!strcmp("int",buff)) {typ=settyp(INT,typ);notdone=1;}
  175.         if(!strcmp("long",buff)) {typ=settyp(LONG,typ);notdone=1;}
  176.         if(!strcmp("float",buff)) {typ=settyp(FLOAT,typ);notdone=1;}
  177.         if(!strcmp("double",buff)) {typ=settyp(DOUBLE,typ);notdone=1;}
  178.         if(!strcmp("const",buff)) {type_qualifiers|=CONST;notdone=1;}
  179.         if(!strcmp("volatile",buff)) {type_qualifiers|=VOLATILE;notdone=1;}
  180.         if(!strcmp("unsigned",buff)) { notdone=1;type_qualifiers|=UNSIGNED;}
  181.         if(!strcmp("signed",buff)) notdone=1; /* hier ueberall strengere Systnaxpruefung */
  182.         if(!strcmp("auto",buff)) {dsc;storage_class=AUTO;notdone=1;}
  183.         if(!strcmp("register",buff)){dsc;storage_class=REGISTER;notdone=1;}
  184.         if(!strcmp("static",buff)) {dsc;storage_class=STATIC;notdone=1;}
  185.         if(!strcmp("extern",buff)) {dsc;storage_class=EXTERN;notdone=1;}
  186.         if(!strcmp("typedef",buff)) {dsc;storage_class=TYPEDEF;notdone=1;}
  187.  
  188.         if(!notdone&&*buff){
  189.             v=find_var(buff,0);
  190.             if(v&&v->storage_class==TYPEDEF){
  191.                 struct struct_declaration *sd;
  192.                 free(new);
  193.                 new=clone_typ(v->vtyp);
  194.                 if((new->flags&15)!=ARRAY&&(new->flags&UNCOMPLETE))
  195.                     if(sd=find_struct((void *)new->exact)){new->flags&=~UNCOMPLETE;new->exact=sd;}
  196.                 typ=new->flags;
  197.                 notdone=1;
  198.             }
  199.         }
  200.         if(DEBUG&2) printf("typ:%d\n",typ);
  201.         killsp();
  202.     }while(notdone);
  203.     s=merk;
  204.     return_sc=storage_class;
  205.     if(typ==0){
  206.         if(storage_class==0&&type_qualifiers==0) {free(new);return(0);}
  207.         typ=INT;
  208.     }
  209.     if(type_qualifiers&1){if(typ>LONG) error(58);}
  210.     if(DEBUG&2) printf("ts finish:%s\n",s);
  211.     new->flags=typ|type_qualifiers;
  212.     return(new);
  213. }
  214.  
  215.  
  216. struct Typ *declarator(struct Typ *a)
  217. /* Erzeugt einen neuen Typ, auf Basis des Typs a            */
  218. /* a wird hiermit verkettet                                 */
  219. {
  220.     struct Typ *t;
  221.     killsp();*ident=0;
  222.     t=direct_declarator(pointer(a));
  223.     if(!a) {if(t) freetyp(t);return(0);} else return(t);
  224. }
  225. struct Typ *pointer(struct Typ *a)
  226. /* Unterroutine fuer declaration(), behandelt Zeiger auf Typ    */
  227. /* Kann sein, dass die Assoziativitaet nicht stimmt */
  228. {
  229.     struct Typ *t;char *merk;int notdone;
  230.     if(!a) return(0);
  231.     killsp();
  232.     while(*s=='*'){
  233.         s++;
  234.         t=(struct Typ *)mymalloc(TYPS);
  235.         if(MDEBUG) printf("malloc %p\n",t);
  236.         t->flags=POINTER;
  237.         t->next=a;
  238.         a=t;
  239.         do{
  240.             killsp();
  241.             merk=s;cpbez(buff);
  242.             notdone=0;
  243.             if(!strcmp("const",buff)) {a->flags|=CONST;notdone=1;}
  244.             if(!strcmp("volatile",buff)) {a->flags|=VOLATILE;notdone=1;}
  245.         }while(notdone);
  246.         s=merk;
  247.     }
  248.     return(a);
  249. }
  250.  
  251. struct Typ *direct_declarator(struct Typ *a)
  252. /*  Unterroutine zu declarator()                    */
  253. /* behandelt [],(funkt),(dekl)                      */
  254. /* Funktionesufrufe und Arrays noch unvollstaendig  */
  255. /* implementiert, auch Assoziativitaet zweifelhaft  */
  256. {
  257.     struct Typ *rek=0,*merk,*p,*t,*first,*last=0;
  258.     struct struct_declaration *fsd;
  259.     char *imerk,fbuff[MAXI];
  260.     killsp();
  261.     if(!isalpha((unsigned char)*s)&&*s!='_'&&*s!='('&&*s!='[') return(a);
  262.     if(isalpha((unsigned char)*s)||*s=='_'){
  263.         cpbez(ident);
  264.         if(!a) return(0);
  265.     }else if(*s=='('&&a){
  266.         /* Rekursion */
  267.         imerk=s; s++; killsp();
  268.         if(*s!=')'&&*ident==0&&!declaration(0)){
  269.             merk=a;
  270.             rek=declarator(a);
  271.             if(*s!=')') error(59); else s++;
  272.         }else s=imerk;
  273.     }
  274.     if(!a)return(0);
  275.     killsp();
  276.     while(*s=='['||*s=='('){
  277.         if(*s=='['){
  278.             s++;
  279.             killsp();
  280.             p=(struct Typ *)mymalloc(TYPS);
  281.             if(MDEBUG) printf("malloc %p\n",p);
  282.             p->flags=ARRAY;
  283.             p->next=0;
  284.             if(*s==']'){
  285.                 p->size=0;
  286.                 p->flags|=UNCOMPLETE;
  287.             }else{
  288.                 np tree;unsigned long l;
  289.                 tree=expression();
  290.                 if(!type_expression(tree)){
  291. /*                    error("incorrect constant expression");*/
  292.                 }else{
  293.                     if(tree->sidefx) error(60);
  294.                     if(tree->flags!=CEXPR||(tree->ntyp->flags&15)<CHAR||(tree->ntyp->flags&15)>LONG){
  295.                         error(19);
  296.                     }else{
  297.                         eval_constn(tree);
  298.                         l=zul2ul(vulong);
  299.                         p->size=l;
  300.                         if(p->size==0) {error(61);p->size=1;}
  301.                     }
  302.                 }
  303.                 free_expression(tree);
  304.             }
  305.             if(*s!=']') error(62); else s++;
  306.             if(last){
  307.                 last->next=p;
  308.                 last=p;
  309.             }else{
  310.                 first=last=p;
  311.             }
  312.         }
  313.         if(*s=='('){
  314.             int komma;
  315.             /* Identifier- oder Parameter-list noch nicht komplett */
  316.             /* z.B. ... oder ohne Parameter                        */
  317.             s++;
  318.             killsp();
  319.             fsd=(struct struct_declaration *)mymalloc(MAXSTRUCTSIZE);
  320.             fsd->count=0;
  321.             imerk=ident;komma=0;
  322.             enter_block();
  323.             while(*s!=')'&&*s!='.'){
  324.                 ident=fbuff;*fbuff=0;komma=0;
  325.                 t=declarator(declaration_specifiers());
  326.                 if(!t&&*ident==0) {error(20);
  327.                                    break;}
  328.                 if(fsd->count){
  329.                     if((t&&!fsd->sl[fsd->count-1].styp)||
  330.                        (!t&&fsd->sl[fsd->count-1].styp))
  331.                         error(63);
  332.                 }
  333.                 if(!return_sc) return_sc=AUTO;
  334.                 if(return_sc!=AUTO&&return_sc!=REGISTER)
  335.                     {error(21);return_sc=AUTO;}
  336.                 fsd->sl[fsd->count].styp=t;
  337.                 fsd->sl[fsd->count].storage_class=return_sc;
  338.                 fsd->sl[fsd->count].identifier=add_identifier(ident,strlen(ident));
  339.                 if(t){
  340.                     if((fsd->sl[fsd->count].styp->flags&15)==VOID&&fsd->count!=0)
  341.                         error(22);
  342.                     /*  Arrays in Zeiger umwandeln  */
  343.                     if((fsd->sl[fsd->count].styp->flags&15)==ARRAY)
  344.                         fsd->sl[fsd->count].styp->flags=POINTER;
  345.                     /*  Funktionen in Zeiger auf Funktionen umwandeln   */
  346.                     if((fsd->sl[fsd->count].styp->flags&15)==FUNKT){
  347.                         struct Typ *new;
  348.                         new=(struct Typ *)mymalloc(TYPS);
  349.                         new->flags=POINTER;
  350.                         new->next=fsd->sl[fsd->count].styp;
  351.                         fsd->sl[fsd->count].styp=new;
  352.                     }
  353.  
  354.                 }
  355.                 fsd->count++;
  356.                 killsp(); /* Hier Syntaxpruefung strenger machen */
  357.                 if(*s==',') {s++;komma=1; killsp();}
  358.             }
  359.             ident=imerk;
  360.             if((*s!='.'||*(s+1)!='.'||*(s+2)!='.')||!komma){
  361.                 if(fsd->count>0&&(!fsd->sl[fsd->count-1].styp||(fsd->sl[fsd->count-1].styp->flags&15)!=VOID)){
  362.                     fsd->sl[fsd->count].styp=(struct Typ *)mymalloc(TYPS);
  363.                     fsd->sl[fsd->count].styp->flags=VOID;
  364.                     fsd->sl[fsd->count].styp->next=0;
  365.                     fsd->sl[fsd->count].identifier=empty;
  366.                     fsd->count++;
  367.                 }
  368.             }else if(komma) {s+=3;komma=0;}
  369.             p=(struct Typ *)mymalloc(TYPS);
  370.             if(MDEBUG) printf("malloc %p\n",p);
  371.             p->flags=FUNKT;
  372.             p->next=0;
  373.             {
  374.                 int m=nesting;
  375.                 nesting=0;
  376.                 p->exact=add_sd(fsd);
  377.                 nesting=m;
  378.             }
  379.             free(fsd);
  380.             killsp();
  381.             if(*s!=')'||komma) error(59); else s++;
  382.             killsp();
  383.             if(*s==','||*s==';'||*s==')'||*s=='=') leave_block();
  384.             if(last){
  385.                 last->next=p;
  386.                 last=p;
  387.             }else{
  388.                 first=last=p;
  389.             }
  390.         }
  391.         killsp();
  392.     }
  393.     if(last){last->next=a;last=a;a=first;}
  394.     if(rek!=0&&rek!=merk){
  395.         /* Zweite Liste anhaengen */
  396.         p=rek;
  397.         while(p->next!=merk) p=p->next;
  398.         if(p) p->next=a; else ierror(0);
  399.         return(rek);
  400.     }
  401.     return(a);
  402. }
  403. int declaration(int offset)
  404. /* Testet, ob nach offset Teichen eine Typangabe kommt  */
  405. /* Hier muss noch Test auf typedefs eingebaut werden */
  406. {
  407.     char *merk=s,buff[MAXI];
  408.     struct Var *v;
  409.     s+=offset;
  410.     killsp();
  411.     /*printf("d:%s\n",s);*/
  412.     cpbez(buff);
  413.     s=merk;
  414.     if(!strcmp("auto",buff)) return(1);
  415.     if(!strcmp("char",buff)) return(1);
  416.     if(!strcmp("const",buff)) return(1);
  417.     if(!strcmp("double",buff)) return(1);
  418.     if(!strcmp("enum",buff)) return(1);
  419.     if(!strcmp("extern",buff)) return(1);
  420.     if(!strcmp("float",buff)) return(1);
  421.     if(!strcmp("int",buff)) return(1);
  422.     if(!strcmp("long",buff)) return(1);
  423.     if(!strcmp("register",buff)) return(1);
  424.     if(!strcmp("short",buff)) return(1);
  425.     if(!strcmp("signed",buff)) return(1);
  426.     if(!strcmp("static",buff)) return(1);
  427.     if(!strcmp("struct",buff)) return(1);
  428.     if(!strcmp("typedef",buff)) return(1);
  429.     if(!strcmp("union",buff)) return(1);
  430.     if(!strcmp("unsigned",buff)) return(1);
  431.     if(!strcmp("void",buff)) return(1);
  432.     if(!strcmp("volatile",buff)) return(1);
  433.     v=find_var(buff,0);
  434.     if(v&&v->storage_class==TYPEDEF) return(1);
  435.     return(0);
  436. }
  437.  
  438. struct struct_declaration *add_sd(struct struct_declaration *decl)
  439. /*  Erzeugt eine Kopie einer struct_declaration und fuegt sie   */
  440. /* in die passende Liste ein; Original kann dann geloescht      */
  441. /* werden.                                                      */
  442. {
  443.     int size;struct struct_declaration *new;
  444.     size=((decl->count)-1)*sizeof(struct struct_list)+sizeof(struct struct_declaration);
  445.     new=(struct struct_declaration *)mymalloc(size);
  446.     memcpy(new,decl,size);
  447.     new->next=0;
  448.     if(first_sd[nesting]==0){
  449.         first_sd[nesting]=last_sd[nesting]=new;
  450.     }else{
  451.         last_sd[nesting]->next=new;
  452.         last_sd[nesting]=new;
  453.     }
  454.     return(last_sd[nesting]); /* return(new) sollte aequiv. sein */
  455. }
  456. void free_sd(struct struct_declaration *p)
  457. /*  Gibt eine struct_declaration-List inkl. struct_lists und    */
  458. /*  allen Typen jeder struct_list frei, nicht aber identifier   */
  459. {
  460.     int i;struct struct_declaration *merk;
  461.     while(p){
  462.         merk=p->next;
  463.         for(i=0;i<p->count;i++) if(p->sl[i].styp) freetyp(p->sl[i].styp);
  464.         free(p);
  465.         p=merk;
  466.     }
  467. }
  468. char *add_identifier(char *identifier,int length)
  469. /*  Kopiert identifier an sicheren Ort, der spaeter zentral     */
  470. /*  freigegeben werden kann.                                    */
  471. /*  Sollte noch einbauen, dass ueberprueft wird, ob schon       */
  472. /*  vorhanden und dann nicht zweimal speichern                  */
  473. {
  474.     struct identifier_list *new;
  475.     if((*identifier==0&&length==0)||identifier==empty) return(empty);
  476.     new=(struct identifier_list*)mymalloc(sizeof(struct identifier_list));
  477.     new->identifier=(char *)mymalloc(length+1);
  478.     memcpy(new->identifier,identifier,length+1);
  479.     new->next=0;new->length=length;
  480.     if(last_ilist[nesting]){
  481.         last_ilist[nesting]->next=new;
  482.         last_ilist[nesting]=new;
  483.     }else{
  484.         last_ilist[nesting]=first_ilist[nesting]=new;
  485.     }
  486.     return(new->identifier);
  487. }
  488. void free_ilist(struct identifier_list *p)
  489. /*  Gibt eine verkettete identifier_liste und saemtliche darin  */
  490. /*  gespeicherten Identifier frei.                              */
  491. {
  492.     struct identifier_list *merk;
  493.     while(p){
  494.         merk=p->next;
  495.         if(p->identifier) free(p->identifier);
  496.         free(p);
  497.         p=merk;
  498.     }
  499. }
  500. int type_uncomplete(struct Typ *p)
  501. /*  Testet, ob Typ unvollstaendig ist. Momentan gelten nur      */
  502. /*  unvollstaendige Strukturen und Arrays von solchen als       */
  503. /*  unvollstaendig, aber keine Zeiger oder Funktionen darauf    */
  504. {
  505.     struct struct_declaration *sd;
  506.     if(!p){ierror(0);return(0);}
  507.     if((p->flags&15)!=ARRAY&&(p->flags&UNCOMPLETE)){
  508.         if(sd=find_struct((void *)p->exact)){p->flags&=~UNCOMPLETE;p->exact=sd;}
  509.     }
  510.     if(p->flags&UNCOMPLETE) return(1);
  511.     if(((p->flags&15)==ARRAY)&&type_uncomplete(p->next)) return(1);
  512.     return(0);
  513. }
  514. void add_struct_identifier(char *identifier,struct struct_declaration *sd)
  515. /*  Erzeugt neuen struct_identifier, fuegt ihn in Liste an und  */
  516. /*  vervollstaendigt unvollstaendige Typen dieser Struktur.     */
  517. {
  518.     struct struct_identifier *new;
  519. /*    struct Typ *t;*/
  520.     if(DEBUG&2) printf("add_si %s\n",identifier);
  521.     new=(struct struct_identifier *)mymalloc(sizeof(struct struct_identifier));
  522.     new->identifier=add_identifier(identifier,strlen(identifier));
  523.     new->sd=sd; new->next=0;
  524.     if(first_si[nesting]==0){
  525.         first_si[nesting]=new;last_si[nesting]=new;
  526.     }else{
  527.         last_si[nesting]->next=new;last_si[nesting]=new;
  528.     }
  529. /*    while(t=find_uncomplete_struct(identifier)){
  530.         printf("Ersetze uncomplete %u\n",t);
  531.         t->flags&=~UNCOMPLETE;t->exact=sd;
  532.     }*/
  533. }
  534. void free_si(struct struct_identifier *p)
  535. /*  Gibt eine struct_identifier-Liste frei, aber nicht die      */
  536. /*  identifiers und struct_declarations                         */
  537. {
  538.     struct struct_identifier *merk;
  539.     while(p){
  540.         merk=p->next;
  541.         free(p);
  542.         p=merk;
  543.     }
  544. }
  545. struct struct_declaration *find_struct(char *identifier)
  546. /*  Sucht angegebene Strukturdefinition und liefert             */
  547. /*  entsprechende struct_declaration                            */
  548. {
  549.     struct struct_identifier *si; int i;
  550.     for(i=nesting;i>=0;i--){
  551.         si=first_si[i];
  552.         while(si){
  553.             if(!strcmp(si->identifier,identifier)) return(si->sd);
  554.             si=si->next;
  555.         }
  556.     }
  557.     return(0);
  558. }
  559. struct Var *add_var(char *identifier, struct Typ *t, int storage_class,struct const_list *clist)
  560. /*  Fuegt eine Variable mit Typ in die var_list ein             */
  561. /*  maschinenspezifisches und Codegeneration fehlen noch        */
  562. /*  Alignment maschinenabhaengig                                */
  563. /*  In der storage_class werden die Flags PARAMETER und evtl.   */
  564. /*  OLDSTYLE erkannt.                                           */
  565. {
  566.     struct Var *new;int f;
  567.     struct struct_declaration *sd;
  568.     /*if(*identifier==0) return;*/ /* sollte woanders bemaekelt werden */
  569.     if(DEBUG&2) printf("add_var(): %s\n",identifier);
  570.     if((t->flags&15)==FUNKT&&((t->next->flags&15)==ARRAY||(t->next->flags&15)==FUNKT))
  571.         error(25);
  572.     if((t->flags&15)!=ARRAY&&(t->flags&UNCOMPLETE))
  573.         if(sd=find_struct((void *)t->exact)){t->flags&=~UNCOMPLETE;t->exact=sd;}
  574.     new=(struct Var *)mymalloc(sizeof(struct Var));
  575.     new->identifier=add_identifier(identifier,strlen(identifier));
  576.     new->clist=clist;
  577.     new->vtyp=t;
  578.     new->storage_class=storage_class&7;
  579.     new->next=0;
  580.     new->flags=0;
  581.     new->fi=0;
  582.     new->nesting=nesting;
  583. /*    if((storage_class&7)==STATIC||(storage_class&7)==EXTERN) new->flags=USEDASSOURCE|USEDASDEST;*/
  584.     if(storage_class&PARAMETER) new->flags=USEDASDEST;
  585.     if((storage_class&7)==REGISTER) new->priority=registerpri; else new->priority=0;
  586.     if(last_var[nesting]){
  587.         new->offset=last_var[nesting]->offset+szof(last_var[nesting]->vtyp);
  588.         last_var[nesting]->next=new;
  589.         last_var[nesting]=new;
  590.     }else{
  591.         new->offset=0;
  592.         first_var[nesting]=last_var[nesting]=new;
  593.     }
  594.     f=t->flags&15;
  595.     if((storage_class&7)==AUTO||(storage_class&7)==REGISTER){
  596.         if(type_uncomplete(t)&&(t->flags&15)!=ARRAY) error(202,identifier);
  597.         /*  das noch ueberpruefen   */
  598.         if((c_flags_val[0].l&2)&&nesting==1&&!(storage_class&PARAMETER)){
  599.             new->offset=max_offset;
  600.         }else{
  601.             new->offset=local_offset[nesting];
  602.         }
  603.         new->offset=((new->offset+align[f]-1)/align[f])*align[f];
  604.         if((storage_class&PARAMETER)&&f>=CHAR&&f<=SHORT){
  605.         /*  Integer-Erweiterungen fuer alle Funktionsparameter  */
  606.             local_offset[nesting]=new->offset+sizetab[INT];
  607.         }else{
  608.             local_offset[nesting]=new->offset+szof(new->vtyp);
  609.         }
  610.         /*  Bei alten Funktionen werden FLOAT als DOUBLE uebergeben */
  611.         if((storage_class&(PARAMETER|OLDSTYLE))==(PARAMETER|OLDSTYLE)&&f==FLOAT)
  612.             local_offset[nesting]+=sizetab[DOUBLE]-sizetab[FLOAT];
  613.  
  614.         if(local_offset[nesting]>max_offset) max_offset=local_offset[nesting];
  615.     }
  616.     if((storage_class&7)==STATIC) new->offset=++label;
  617.     if(storage_class&PARAMETER){
  618. /*        new->storage_class&=~PARAMETER;*/
  619.         /* ob das hier so funktioniert ? Bei BIGENDIAN nimmt man den    */
  620.         /* hinteren Teil des INTs, bei LOWENDIAN muesste man eigentlich */
  621.         /* nichts tun, oder? Datenformate, bei denen wirklich           */
  622.         /* umgewandelt werden muss, werden (noch?) nicht unterstuetzt.  */
  623.         /* Ob sowas aber ueberhaupt zulaessig waere, weiss ich nicht.   */
  624.         if(f>=CHAR&&f<=SHORT&&sizetab[f]<sizetab[INT]){
  625.             if(BIGENDIAN)
  626.                 new->offset+=sizetab[INT]-sizetab[f];
  627.         }
  628.         if((storage_class&OLDSTYLE)&&f==FLOAT){
  629.         /*  Bei alten Funktionen werden DOUBLE nach FLOAT konvertiert   */
  630.             struct IC *conv=(struct IC *)mymalloc(ICS);
  631.             conv->code=CONVDOUBLE;
  632.             conv->typf=FLOAT;
  633.             conv->q1.flags=VAR|DONTREGISTERIZE;
  634.             conv->z.flags=VAR;
  635.             conv->q2.flags=0;
  636.             conv->q1.v=conv->z.v=new;
  637.             conv->q1.val.vlong=conv->z.val.vlong=l2zl(0);
  638.             add_IC(conv);
  639.             new->flags|=CONVPARAMETER;
  640.         }
  641.         new->offset=-new->offset;
  642.     }
  643.     return(new);
  644. }
  645. void free_fi(struct function_info *p)
  646. /*  Gibt ein function_info mit Inhalt frei  */
  647. {
  648.     if(p->first_ic) free_IC(p->first_ic);
  649.     if(p->vars) free_var(p->vars);
  650.     free(p);
  651. }
  652. void free_var(struct Var *p)
  653. /*  Gibt Variablenliste inkl. Typ, aber ohne Identifier frei    */
  654. {
  655.     struct Var *merk;
  656.     while(p){
  657.         merk=p->next;
  658.         if(!(p->flags&USEDASADR)&&(p->storage_class==AUTO||p->storage_class==REGISTER)){
  659.             if(*p->identifier&&!(p->flags&USEDASDEST)&&(p->vtyp->flags&15)<=POINTER) error(64,p->identifier);
  660.             if(*p->identifier&&!(p->flags&USEDASSOURCE)&&(p->vtyp->flags&15)<=POINTER) error(65,p->identifier);
  661.         }
  662.         if(DEBUG&2) printf("free_var %s, pri=%d\n",p->identifier,p->priority);
  663.         if(p->vtyp) freetyp(p->vtyp);
  664.         if(p->clist) free_clist(p->clist);
  665.         if(p->fi){
  666.             if(DEBUG&2) printf("free_fi of function %s\n",p->identifier);
  667.             free_fi(p->fi);
  668.             if(DEBUG&2) printf("end free_fi of function %s\n",p->identifier);
  669.         }
  670.         free(p);
  671.         p=merk;
  672.     }
  673. }
  674. struct Var *find_var(char *identifier,int endnesting)
  675. /*  sucht Variable mit Bezeichner und liefert Zeiger zurueck    */
  676. /*  es werden nur Variablen der Bloecke endnesting-nesting      */
  677. /*  durchsucht                                                  */
  678. {
  679.     int i;struct Var *v;
  680.     if(*identifier==0||identifier==0) return(0);
  681.     for(i=nesting;i>=endnesting;i--){
  682.         v=first_var[i];
  683.         while(v){
  684.             if(!strcmp(v->identifier,identifier)) return(v);
  685.             v=v->next;
  686.         }
  687.     }
  688.     return(0);
  689. }
  690. void var_declaration(void)
  691. /*  Bearbeitet eine Variablendeklaration und erzeugt alle       */
  692. /*  noetigen Strukturen                                         */
  693. {
  694.     struct Typ *ts,*t,*old=0,*om=0;char *imerk,vident[MAXI];
  695.     int mdef=0,makeint=0,notdone,storage_class,msc,extern_flag,isfunc,had_decl;
  696.     struct Var *v;
  697.     ts=declaration_specifiers();notdone=1;
  698.     storage_class=return_sc;
  699.     if(storage_class==EXTERN) extern_flag=1; else extern_flag=0;
  700.     killsp();
  701.     if(*s==';'){
  702.         if(storage_class||((ts->flags&15)!=STRUCT&&(ts->flags&15)!=UNION&&(ts->flags&15)!=INT))
  703.             error(36);
  704.         freetyp(ts);s++;killsp();
  705.         return;
  706.     }
  707.     if(nesting==0&&(storage_class==AUTO||storage_class==REGISTER))
  708.         {error(66);storage_class=EXTERN;}
  709.     if(!ts){
  710.         if(nesting<=1){
  711.             ts=(struct Typ *)mymalloc(TYPS);
  712.             ts->flags=INT;ts->next=0;
  713.             makeint=1;
  714.             if(!storage_class) storage_class=EXTERN;
  715.             error(67);
  716.         }else{
  717.             ierror(0);return;
  718.         }
  719.     }
  720.     if(storage_class==0){
  721.         if(nesting==0) storage_class=EXTERN; else storage_class=AUTO;
  722.     }
  723.     msc=storage_class;
  724.     while(notdone){
  725.         int oldnesting=nesting;
  726.         imerk=ident;ident=vident;*vident=0;  /* merken von ident hier vermutlich */
  727.         storage_class=msc;
  728.         if(old) {freetyp(old);old=0;}
  729.         t=declarator(clone_typ(ts));
  730.         if((t->flags&15)!=FUNKT) isfunc=0;
  731.             else {isfunc=1;if(storage_class!=STATIC) storage_class=EXTERN;}
  732.         ident=imerk;                    /* nicht unbedingt noetig ?         */
  733.         v=find_var(vident,oldnesting);
  734.         if(v){
  735.             had_decl=1;
  736.             if(nesting>0&&(v->flags&DEFINED)&&!extern_flag&&!isfunc){
  737.                 error(27,vident);
  738.             }else{
  739.                 if(t&&v->vtyp&&!compare_pointers(v->vtyp,t,255)){
  740.                     error(68,vident);
  741.                 }
  742.                 if(storage_class!=v->storage_class&&!extern_flag)
  743.                     error(28,v->identifier);
  744.                 if(!isfunc&&!extern_flag) v->flags|=TENTATIVE;
  745.             }
  746.             if(!isfunc){
  747.                 v->vtyp=t;
  748.             }else{
  749.                 om=v->vtyp;
  750.                 if(t->exact->count>0) {old=v->vtyp;v->vtyp=t;}
  751.             }
  752.         }else{
  753.             had_decl=0;
  754.             if(isfunc&&*s!=','&&*s!=';'&&*s!=')'&&*s!='='&&nesting>0) nesting--;
  755.             v=add_var(vident,t,storage_class,0);
  756.             if(isfunc&&*s!=','&&*s!=';'&&*s!=')'&&*s!='='&&nesting>=0) nesting++;
  757.             if(!v) ierror(0);
  758.             else{
  759.                 if(!isfunc&&!extern_flag){
  760.                     v->flags|=TENTATIVE;
  761.                     if(nesting>0) v->flags|=DEFINED;
  762.                 }
  763.             }
  764.             om=0;
  765.         }
  766.         killsp();
  767.         /*  Initialisierung von Variablen bei Deklaration   */
  768.         if(*s=='='){
  769.             s++;killsp();
  770.             if(!had_decl&&v->nesting==0&&v->storage_class==EXTERN)
  771.                 error(168,v->identifier);
  772.             if(v->flags&DEFINED) {if(nesting==0) error(30,v->identifier);}
  773.                 else v->flags|=DEFINED;
  774.             if(v->storage_class==TYPEDEF) error(114,v->identifier);
  775.             if(extern_flag){
  776.                 error(118,v->identifier);
  777.                 if(v->storage_class!=EXTERN){ error(77);v->storage_class=EXTERN;}
  778.             }
  779.             v->clist=initialization(v->vtyp,v->storage_class==AUTO||v->storage_class==REGISTER);
  780.             if(v->clist){
  781.                 if((v->vtyp->flags&15)==ARRAY&&v->vtyp->size==0){
  782.                     struct const_list *p=v->clist;
  783.                     if(!(v->vtyp->flags&UNCOMPLETE)) ierror(0);
  784.                      else v->vtyp->flags&=~UNCOMPLETE;
  785.                     while(p){v->vtyp->size++;p=p->next;}
  786.                     local_offset[nesting]+=szof(v->vtyp);
  787.                     if(local_offset[nesting]>max_offset) max_offset=local_offset[nesting];
  788.                 }
  789.                 if(v->storage_class==AUTO||v->storage_class==REGISTER){
  790.                     struct IC *new;
  791.                 /*  Initialisierung von auto-Variablen  */
  792.                     new=(struct IC *)mymalloc(ICS);
  793.                     new->code=ASSIGN;
  794.                     new->typf=v->vtyp->flags;
  795.                     new->q2.flags=0;
  796.                     new->q2.reg=szof(v->vtyp);
  797.                     new->z.flags=VAR;
  798.                     new->z.v=v;
  799.                     new->z.val.vlong=l2zl(0L);
  800.                     if(v->clist->tree){
  801.                     /*  einzelner Ausdruck  */
  802.                         gen_IC(v->clist->tree,0,0);
  803.                         convert(v->clist->tree,v->vtyp->flags&31);
  804.                         new->q1=v->clist->tree->o;
  805. /*                        v->clist=0;*/
  806.                     }else{
  807.                     /*  Array etc.  */
  808.                         struct Var *nv;
  809.                         nv=add_var(empty,clone_typ(v->vtyp),STATIC,v->clist);
  810.                         nv->flags|=DEFINED;
  811.                         nv->vtyp->flags|=CONST;
  812. /*                        v->clist=0;*/
  813.                         new->q1.flags=VAR;
  814.                         new->q1.v=nv;
  815.                         new->q1.val.vlong=l2zl(0L);
  816.                     }
  817.                     add_IC(new);
  818. /*                    if(v->clist&&v->clist->tree){free_expression(v->clist->tree);v->clist->tree=0;}*/
  819.                 }
  820.             }
  821.         }else{
  822.             if((v->flags&DEFINED)&&type_uncomplete(v->vtyp)) error(202,v->identifier);
  823.             if((v->vtyp->flags&CONST)&&v->storage_class!=TYPEDEF&&!extern_flag)
  824.                 error(119,v->identifier);
  825.         }
  826.         if(*s==',') {s++;killsp();mdef=1;} else notdone=0;
  827.     }
  828.     freetyp(ts);
  829.     if(!mdef&&t&&(t->flags&15)==FUNKT&&*s!=';'){
  830.     /*  Funktionsdefinition                                     */
  831.         int i,oldstyle=0;
  832.         if(DEBUG&1) printf("Funktionsdefinition!\n");
  833.         if(only_inline==2) only_inline=0;
  834.         if(nesting<1) ierror(0);
  835.         if(nesting>1) error(32);
  836.         if(v->flags&DEFINED) error(33,v->identifier);
  837.             else v->flags|=DEFINED;
  838.         if(storage_class!=EXTERN&&storage_class!=STATIC) error(34);
  839.         if(extern_flag) error(120);
  840.         if(!strcmp(v->identifier,"main")&&(!t->next||t->next->flags!=INT)) error(121);
  841.         if(!had_decl&&v->nesting==0&&v->storage_class==EXTERN)
  842.             error(168,v->identifier);
  843.         while(*s!='{'){
  844.         /*  alter Stil  */
  845.             struct Typ *nt=declaration_specifiers();notdone=1;oldstyle=OLDSTYLE;
  846.             if(!ts) {error(35);}
  847.             while(notdone){
  848.                 int found=0;
  849.                 imerk=ident;ident=vident;*vident=0;
  850.                 ts=declarator(clone_typ(nt));
  851.                 ident=imerk;
  852.                 if(!ts) {error(36);}
  853.                 else{
  854.                     for(i=0;i<t->exact->count;i++){
  855.                         if(!strcmp(t->exact->sl[i].identifier,vident)){
  856.                             found=1;
  857.                             if(t->exact->sl[i].styp){
  858.                                 error(69,vident);
  859.                                 freetyp(t->exact->sl[i].styp);
  860.                             }
  861.                             /*  typ[] in *typ   */
  862.                             if((ts->flags&15)==ARRAY) ts->flags=POINTER;
  863.                             /*  typ() in *typ() */
  864.                             if((ts->flags&15)==FUNKT){
  865.                                 struct Typ *new=(struct Typ *)mymalloc(TYPS);
  866.                                 new->flags=POINTER;
  867.                                 new->next=ts;
  868.                                 ts=new;
  869.                             }
  870.                             if(!return_sc) return_sc=AUTO;
  871.                             if(return_sc!=AUTO&&return_sc!=REGISTER)
  872.                                 {error(122);return_sc=AUTO;}
  873.                             t->exact->sl[i].storage_class=return_sc;
  874.                             t->exact->sl[i].styp=ts;
  875.                         }
  876.                     }
  877.                 }
  878.                 if(!found) {error(37,vident);}
  879.                 killsp();
  880.                 if(*s==',') {s++;killsp();} else notdone=0;
  881.             }
  882.             if(nt) freetyp(nt);
  883.             if(*s==';'){s++;killsp();
  884.             }else{
  885.                 error(54);
  886.                 while(*s!='{'&&*s!=';'){s++;killsp();}
  887.             }
  888.         }
  889.         if(t->exact->count==0){
  890.             /*  das hier setzt noch voraus, das das Array in    */
  891.             /*  struct_declaration 1 Element hat                */
  892.             struct struct_declaration sd;
  893.             if(DEBUG&1) printf("prototype converted to (void)\n");
  894.             sd.count=1;
  895.             sd.sl[0].identifier=empty;
  896.             sd.sl[0].storage_class=AUTO;
  897.             sd.sl[0].styp=mymalloc(TYPS);
  898.             sd.sl[0].styp->flags=VOID;
  899.             sd.sl[0].styp->next=0;
  900.             nesting--;
  901.             t->exact=add_sd(&sd);
  902.             nesting++;
  903.         }
  904.         if(om&&!compare_sd(om->exact,t->exact))
  905.             error(123);
  906.         nocode=0;currentpri=1;
  907. /*        enter_block();*/
  908.         local_offset[1]=4;
  909.         return_var=0;
  910.         if(!v->vtyp) ierror(0);
  911.         if(v->vtyp->next->flags==VOID) return_typ=0;
  912.         else{
  913.             return_typ=v->vtyp->next;
  914.             if(!freturn(return_typ)){
  915. #ifdef OLDPARMS
  916.                 return_var=add_var(empty,clone_typ(return_typ),STATIC,0);
  917.                 return_var->flags|=DEFINED;
  918. #else
  919.                 /*  Parameter fuer die Rueckgabe von Werten, die nich in einem  */
  920.                 /*  Register sind.                                              */
  921.                 struct Typ *rt=mymalloc(TYPS);
  922.                 rt->flags=POINTER;rt->next=return_typ;
  923.                 return_var=add_var(empty,clone_typ(rt),AUTO|PARAMETER|oldstyle,0);
  924.                 return_var->flags|=DEFINED;
  925.                 free(rt);
  926. #endif
  927.             }
  928.         }
  929.         first_ic=last_ic=0;ic_count=0;
  930.         for(i=0;i<t->exact->count;i++){
  931.             if(!t->exact->sl[i].styp&&*t->exact->sl[i].identifier){
  932.                 struct Typ *nt;
  933.                 nt=(struct Typ *)mymalloc(TYPS);
  934.                 nt->flags=INT; nt->next=0;
  935.                 t->exact->sl[i].styp=nt;
  936.                 t->exact->sl[i].storage_class=AUTO;
  937.                 error(124);
  938.             }
  939.             if(*t->exact->sl[i].identifier){
  940.                 struct Var *tmp;
  941.                 tmp=add_var(t->exact->sl[i].identifier,clone_typ(t->exact->sl[i].styp),t->exact->sl[i].storage_class|PARAMETER|oldstyle,0);
  942.                 tmp->flags|=DEFINED;
  943.                 if(oldstyle){
  944.                     freetyp(t->exact->sl[i].styp);
  945.                     t->exact->sl[i].styp=0; /*  Prototype entfernen */
  946.                 }
  947.             }
  948.         }
  949.         if(oldstyle) t->exact->count=0; /*  Prototype entfernen */
  950.         local_offset[1]=0;
  951.         return_label=++label;
  952.         v->flags|=GENERATED;
  953.         {int i;
  954.             for(i=1;i<=MAXR;i++) {regs[i]=regused[i]=regsa[i];regsbuf[i]=0;}
  955.         }
  956.         max_offset=0;function_calls=0;float_used=0;has_return=0;goto_used=0;
  957.         compound_statement();
  958.         if((v->vtyp->next->flags&15)!=VOID&&!has_return){
  959.             if(strcmp(v->identifier,"main")) error(173,v->identifier);
  960.                 else error(174,v->identifier);
  961.         }
  962.         {int i;
  963.             for(i=1;i<=MAXR;i++) if(regs[i]!=regsa[i]) {printf("Register %s:\n",regnames[i]);ierror(0);}
  964.         }
  965.         gen_label(return_label);
  966. #ifdef OLDPARMS
  967.         if(return_var){
  968.         /*  Zeiger in Returnregister schreiben, bei Funktionen, deren   */
  969.         /*  Rueckgabewert nicht in Registern uebergeben wird            */
  970.             int rreg;
  971.             struct Typ *pointer=(struct Typ *)mymalloc(TYPS);
  972.             struct IC *new=(struct IC *)mymalloc(ICS);
  973.             pointer->flags=POINTER;pointer->next=0;
  974.             rreg=freturn(pointer);
  975.             free(pointer);
  976.             if(regs[rreg]) ierror(0);
  977.             new->code=ALLOCREG;
  978.             new->q1.flags=REG;
  979.             new->q2.flags=new->z.flags=0;
  980.             new->typf=0;
  981.             new->q1.reg=rreg;
  982.             add_IC(new);
  983.             regs[rreg]=1;
  984.             new=(struct IC *)mymalloc(ICS);
  985.             new->code=ASSIGN;
  986.             new->typf=POINTER;
  987.             new->q1.flags=SCRATCH|VAR|VARADR;
  988.             new->q1.reg=0;
  989.             new->q1.v=return_var;
  990.             new->q1.val.vlong=l2zl(0L);
  991.             new->q2.flags=0;
  992.             new->q2.reg=sizetab[POINTER];
  993.             new->z.flags=REG;
  994.             new->z.reg=rreg;
  995.             add_IC(new);
  996.             free_reg(rreg);
  997.         }
  998. #endif
  999.         if(first_ic&&errors==0){
  1000.             if((c_flags[2]&USEDFLAG)&&ic1){fprintf(ic1,"function %s\n",v->identifier); pric(ic1,first_ic);}
  1001. /*            if((c_flags[0]&USEDFLAG)&&(c_flags_val[0].l&1)) simple_regs();*/
  1002.             if(c_flags[0]&USEDFLAG) optimize(c_flags_val[0].l,v);
  1003.             if((c_flags[3]&USEDFLAG)&&ic2){fprintf(ic2,"function %s\n",v->identifier); pric(ic2,first_ic);}
  1004.             if(out&&!only_inline&&!(c_flags[5]&USEDFLAG)){
  1005.                 gen_code(out,first_ic,v,max_offset);
  1006.             }
  1007. /*            if(DEBUG&8192){fprintf(ic2,"function %s, after gen_code\n",v->identifier); pric(ic2,first_ic);}*/
  1008.             free_IC(first_ic);
  1009.             first_ic=last_ic=0;
  1010.         }
  1011.         if(v->fi&&v->fi->first_ic){
  1012.             struct Var *vp;
  1013.             if(DEBUG&1) printf("leave block %d (inline-version)\n",nesting);
  1014.             if(nesting!=1) ierror(0);
  1015.             if(merk_varl) merk_varl->next=first_var[nesting]; else merk_varf=first_var[nesting];
  1016.             if(last_var[nesting]) merk_varl=last_var[nesting];
  1017.             if(merk_sil) merk_sil->next=first_si[nesting]; else merk_sif=first_si[nesting];
  1018.             if(last_si[nesting]) merk_sil=last_si[nesting];
  1019.             if(merk_sdl) merk_sdl->next=first_sd[nesting]; else merk_sdf=first_sd[nesting];
  1020.             if(last_sd[nesting]) merk_sdl=last_sd[nesting];
  1021.             if(merk_ilistl) merk_ilistl->next=first_ilist[nesting]; else merk_ilistf=first_ilist[nesting];
  1022.             if(last_ilist[nesting]) merk_ilistl=last_ilist[nesting];
  1023.  
  1024.             if(merk_varf&&!only_inline) gen_vars(merk_varf);
  1025.             if(first_llist) free_llist(first_llist);
  1026.             if(first_clist) free_clist(first_clist);
  1027.             if(merk_sif) free_si(merk_sif);
  1028.             if(merk_sdf) free_sd(merk_sdf);
  1029.             /*  hier noch was ueberlegen    */
  1030. /*            if(merk_ilistf) free_ilist(merk_ilistf);*/
  1031.             nesting--;
  1032.             v->fi->vars=merk_varf;
  1033. /*            v->fi->vars=first_var[1];*/
  1034.             /*  keine echten Parameter=>keine negativen Offsets */
  1035. /*            vp=first_var[1];*/
  1036.             vp=merk_varf;
  1037.             while(vp){
  1038.                 if(vp->storage_class==AUTO||vp->storage_class==REGISTER){
  1039.                     if(vp->offset<0){
  1040.                         vp->offset=0;
  1041.                         if(DEBUG&1024) printf("converted parameter <%s>(%d) for inlining\n",vp->identifier,vp->offset);
  1042.                     }else vp->offset=4;
  1043.                 }
  1044.                 vp=vp->next;
  1045.             }
  1046.         }else{
  1047.             leave_block();
  1048.         }
  1049.         if(only_inline==2) only_inline=0;
  1050.     }else{
  1051.         if(makeint) error(125);
  1052.         if(*s==';') s++; else error(54);
  1053.         if((t->flags&15)==FUNKT&&t->exact){
  1054.             struct struct_declaration *sd=t->exact;int i,f;
  1055.             for(f=0,i=0;i<sd->count;i++)
  1056.                 if(!sd->sl[i].styp){error(126);f=1;}
  1057.             if(f){
  1058.                 for(i=0;i<sd->count;i++) if(sd->sl[i].styp) freetyp(sd->sl[i].styp);
  1059.                 sd->count=0;
  1060.             }
  1061.         }
  1062.     }
  1063.     if(old) freetyp(old);
  1064. }
  1065. int storage_class_specifiers(void)
  1066. /*  Gibt angegebene storage_class zurueck                   */
  1067. /*  muss nach ANSI wohl in declaration_specifiers           */
  1068. /*  integriert werden                                       */
  1069. {
  1070.     char *merk=s,buff[MAXI];
  1071.     killsp();cpbez(buff);
  1072.     if(!strcmp("auto",buff)) return(AUTO);
  1073.     if(!strcmp("register",buff)) return(REGISTER);
  1074.     if(!strcmp("static",buff)) return(STATIC);
  1075.     if(!strcmp("extern",buff)) return(EXTERN);
  1076.     if(!strcmp("typedef",buff)) return(TYPEDEF);
  1077.     s=merk;return(0);
  1078. }
  1079. struct Typ *clone_typ(struct Typ *old)
  1080. /*  Erzeugt Kopie eines Typs und liefert Zeiger auf Kopie   */
  1081. {
  1082.     struct Typ *new;
  1083.     if(!old) return(0);
  1084.     new=(struct Typ *)mymalloc(TYPS);
  1085.     *new=*old;
  1086.     if(new->next) new->next=clone_typ(new->next);
  1087.     return(new);
  1088. }
  1089. int compare_pointers(struct Typ *a,struct Typ *b,int qual)
  1090. /*  vergleicht, ob Typ beider Typen gleich ist, const/volatile      */
  1091. /*  werden laut ANSI nicht beruecksichtigt                          */
  1092. {
  1093.     struct struct_declaration *sd;
  1094.     int af=a->flags&qual,bf=b->flags&qual;
  1095.     if(af!=bf) return(0);
  1096.     af&=15;bf&=15;
  1097.     if(af!=ARRAY&&(a->flags&UNCOMPLETE))
  1098.         if(sd=find_struct((void *)a->exact)){a->flags&=~UNCOMPLETE;a->exact=sd;}
  1099.     if(bf!=ARRAY&&(b->flags&UNCOMPLETE))
  1100.         if(sd=find_struct((void *)b->exact)){b->flags&=~UNCOMPLETE;b->exact=sd;}
  1101.     if(af==FUNKT){
  1102.         if(a->exact->count&&!compare_sd(a->exact,b->exact)) return(0);
  1103.     }
  1104.     if(af==STRUCT||af==UNION){
  1105.         if((a->flags&UNCOMPLETE)!=(b->flags&UNCOMPLETE)) return(0);
  1106.         if(a->flags&UNCOMPLETE){
  1107.             if(strcmp((char *)a->exact,(char *)b->exact)) return(0);
  1108.         }else{
  1109.             if(a->exact!=b->exact) return(0);
  1110.         }
  1111.     }
  1112.     if(af==ARRAY){
  1113.         if(a->size&&b->size&&a->size!=b->size) return(0);
  1114.     }
  1115.     if(a->next==0&&b->next!=0) return(0);
  1116.     if(a->next!=0&&b->next==0) return(0);
  1117.     if(a->next==0&&b->next==0) return(1);
  1118.     return(compare_pointers(a->next,b->next,qual));
  1119. }
  1120. int compare_sd(struct struct_declaration *a,struct struct_declaration *b)
  1121. /*  Vergleicht, ob zwei struct_declarations identisch sind          */
  1122. /*  Wird nur nur fuer Prototypen benutzt, leere Liste immer gleich  */
  1123. {
  1124.     int i;
  1125.     if(!a->count||!b->count) return(1);
  1126.     if(a->count!=b->count) return(0);
  1127.     for(i=0;i<a->count;i++)
  1128.         if(a->sl[i].styp&&b->sl[i].styp&&!compare_pointers(a->sl[i].styp,b->sl[i].styp,255)) return(0);
  1129.     return(1);
  1130. }
  1131. void free_clist(struct const_list *p)
  1132. /*  gibt clist frei                                         */
  1133. {
  1134.     struct const_list *merk;
  1135.     return;
  1136.     while(p){
  1137.         merk=p->next;
  1138.         if(p->other) free_clist(p->other);
  1139.         if(p->tree) free_expression(p->tree);
  1140.         free(p);
  1141.         p=merk;
  1142.     }
  1143. }
  1144. void gen_clist(FILE *,struct Typ *,struct const_list *);
  1145.  
  1146. void gen_vars(struct Var *v)
  1147. /*  generiert Variablen                                     */
  1148. {
  1149.     int mode,al;struct Var *p;
  1150.     if(errors!=0||(c_flags[5]&USEDFLAG)) return;
  1151.     for(mode=0;mode<3;mode++){
  1152.         for(al=maxalign;al>=1;al--){
  1153.             int i,flag;
  1154.             for(i=1,flag=0;i<15;i++) if(align[i]==al) flag=1;
  1155.             if(!flag) continue;
  1156.             gen_align(out,al);
  1157.             for(p=v;p;p=p->next){
  1158.                 if(DEBUG&2) printf("gen_var(): %s\n",p->identifier);
  1159.                 if(p->storage_class==STATIC||p->storage_class==EXTERN){
  1160.                     if(!(p->flags&GENERATED)){
  1161.                         if(p->storage_class==EXTERN&&!(p->flags&(USEDASSOURCE|USEDASDEST))&&!(p->flags&(TENTATIVE|DEFINED))) continue;
  1162.                         if((p->vtyp->flags&15)!=ARRAY){
  1163.                             if(align[p->vtyp->flags&15]!=al) continue;
  1164.                         }else{
  1165.                             if(align[p->vtyp->next->flags&15]!=al) continue;
  1166.                         }
  1167.                         /*  erst konstante initialisierte Daten */
  1168.                         if(mode==0){
  1169.                             if(!p->clist) continue;
  1170.                             if(!(p->vtyp->flags&(CONST|STRINGCONST))){
  1171.                                 struct Typ *t=p->vtyp;int f=0;
  1172.                                 do{
  1173.                                     if(t->flags&(CONST|STRINGCONST)) break;
  1174.                                     if((t->flags&15)!=ARRAY){f=1;break;}
  1175.                                     t=t->next;
  1176.                                 }while(1);
  1177.                                 if(f) continue;
  1178.                             }
  1179.                         }
  1180.                         /*  dann initiolisierte */
  1181.                         if(mode==1&&!p->clist) continue;
  1182.                         /*  und dann der Rest   */
  1183.                         if(mode==2&&p->clist) continue;
  1184.                         gen_var_head(out,p);
  1185.                         if(!(p->flags&(TENTATIVE|DEFINED))){
  1186.                             if(p->storage_class==STATIC) error(127,p->identifier);
  1187.                             continue;
  1188.                         }
  1189.                         if(!p->clist){
  1190.                             if(type_uncomplete(p->vtyp)) error(202,p->identifier);
  1191.                             gen_ds(out,szof(p->vtyp),p->vtyp);
  1192.                         }else{
  1193.                             gen_clist(out,p->vtyp,p->clist);
  1194.                         }
  1195.                         p->flags|=GENERATED;
  1196.                     }
  1197.                 }
  1198.             }
  1199.         }
  1200.     }
  1201. }
  1202. void gen_clist(FILE *f,struct Typ *t,struct const_list *cl)
  1203. /*  generiert dc fuer const_list                            */
  1204. /*  hier ist noch einiges zu tun                            */
  1205. {
  1206.     int i;
  1207.     if((t->flags&15)==ARRAY){
  1208.         for(i=0;i<t->size&&cl;i++,cl=cl->next){
  1209.             if(!cl->other){ierror(0);return;}
  1210.             gen_clist(f,t->next,cl->other);
  1211.         }
  1212.         if(i<t->size) gen_ds(f,(t->size-i)*szof(t->next),t->next);
  1213.         return;
  1214.     }
  1215.     if((t->flags&15)==UNION){
  1216.         long rest;
  1217.         gen_clist(f,t->exact->sl[0].styp,cl);
  1218.         rest=szof(t)-szof(t->exact->sl[0].styp);
  1219.         if(rest) gen_ds(f,rest,0);
  1220.         return;
  1221.     }
  1222.     if((t->flags&15)==STRUCT){
  1223.         int size=0,al;struct Typ *st;
  1224.         for(i=0;i<t->exact->count&&cl;i++){
  1225.             if(!cl->other){ierror(0);return;}
  1226.             st=t->exact->sl[i].styp;
  1227.             al=align[st->flags&15];
  1228.             if(size%al) {gen_ds(f,al-size%al,0);size+=al-size%al;}
  1229.             if(!t->exact->sl[i].identifier) ierror(0);
  1230.             if(t->exact->sl[i].identifier[0]){
  1231.                 gen_clist(f,st,cl->other);
  1232.                 cl=cl->next;
  1233.             }else{
  1234.                 gen_ds(f,szof(st),0); /* sollte unnamed bitfield sein */
  1235.             }
  1236.             size+=szof(st);
  1237.         }
  1238.         for(;i<t->exact->count;i++){
  1239.             st=t->exact->sl[i].styp;
  1240.             al=align[st->flags&15];
  1241.             if(size%al) {gen_ds(f,al-size%al,0);size+=al-size%al;}
  1242.             gen_ds(f,szof(t->exact->sl[i].styp),t->exact->sl[i].styp);
  1243.             size+=szof(st);
  1244.         }
  1245.         al=align[STRUCT];
  1246.         if(size%al) gen_ds(f,al-size%al,0);
  1247.         return;
  1248.     }
  1249.     gen_dc(f,t->flags&31,cl);
  1250. }
  1251. struct const_list *initialization(struct Typ *t,int noconst)
  1252. /*  traegt eine Initialisierung in eine const_list ein          */
  1253. {
  1254.     struct const_list *first,*cl,**prev;np tree,tree2;int i,bracket;
  1255.     if((t->flags&15)==FUNKT){error(42);return(0);}
  1256.     if((t->flags&15)==ARRAY){
  1257.         if(*s=='\"'&&t->next&&(t->next->flags&15)==CHAR){
  1258.             killsp();
  1259.             tree=string_expression();
  1260.             first=(struct const_list *)tree->identifier;
  1261.             free_expression(tree);
  1262.             return(first);
  1263.         }
  1264.         if(*s=='{'){s++;killsp();bracket=1;} else bracket=0;
  1265.         prev=0;
  1266.         for(i=0;(!t->size||i<t->size)&&*s!='}';i++){
  1267.             cl=(struct const_list *)mymalloc(CLS);
  1268.             cl->next=0;cl->tree=0;
  1269.             cl->other=initialization(t->next,0);
  1270.             killsp();
  1271.             if(*s==','){s++;killsp();}
  1272.             if(prev) *prev=cl; else first=cl;
  1273.             prev=&cl->next;
  1274.         }
  1275.         if(bracket)
  1276.             if(*s=='}'){s++;killsp();} else error(128);
  1277.         return(first);
  1278.     }
  1279.     if((t->flags&15)==STRUCT&&(*s=='{'||!noconst)){
  1280.         if(t->flags&UNCOMPLETE)
  1281.             {error(43);return(0);}
  1282.         if(*s=='{'){s++;killsp();bracket=1;} else bracket=0;
  1283.         prev=0;
  1284.         for(i=0;i<t->exact->count&&*s!='}';i++){
  1285.             if(t->exact->sl[i].identifier[0]==0) {continue;} /* unnamed bitfield */
  1286.             cl=(struct const_list *)mymalloc(CLS);
  1287.             cl->next=0;cl->tree=0;
  1288.             cl->other=initialization(t->exact->sl[i].styp,0);
  1289.             if(*s==','){s++;killsp();}
  1290.             if(prev) *prev=cl; else first=cl;
  1291.             prev=&cl->next;
  1292.         }
  1293.         if(bracket)
  1294.             if(*s=='}'){s++;killsp();} else error(128);
  1295.         return(first);
  1296.     }
  1297.     if((t->flags&15)==UNION&&(*s=='{'||!noconst)){
  1298.         if(t->flags&UNCOMPLETE)
  1299.             {error(44);return(0);}
  1300.         if(*s=='{'){s++;killsp();bracket=1;} else bracket=0;
  1301.         first=initialization(t->exact->sl[0].styp,0);
  1302.         if(bracket)
  1303.             if(*s=='}'){s++;killsp();} else error(128);
  1304.         return(first);
  1305.     }
  1306.     tree2=tree=assignment_expression();
  1307.     if(!tree){error(45);return(0);}
  1308.     if(!type_expression(tree)){free_expression(tree); return(0);}
  1309.     tree=makepointer(tree);
  1310.     test_assignment(t,tree);
  1311.     if(!noconst){
  1312.     /*  nur Konstanten erlaubt (bei Arrays/Strukturen etc. oder static) */
  1313.         if(tree->flags!=CEXPR){
  1314.             while(tree->flags==CAST) tree=tree->left;
  1315.             if(tree->flags==ADDRESS||tree->flags==ADDRESSS||tree->flags==ADDRESSA){
  1316.                 gen_IC(tree,0,0);
  1317.                 if(!(tree->o.flags&VARADR)){
  1318.                 /*  hier fehlen noch viele Pruefungen   */
  1319.                     free_expression(tree);error(46);
  1320.                     return(0);
  1321.                 }
  1322.                 first=(struct const_list *)mymalloc(CLS);
  1323.                 first->next=first->other=0;
  1324.                 first->tree=tree;
  1325.                 killsp();
  1326.                 return(first);
  1327.             }else{
  1328.                 free_expression(tree);error(46);
  1329.                 return(0);
  1330.             }
  1331.         }
  1332.         first=(struct const_list *)mymalloc(CLS);
  1333.         first->next=first->other=0;
  1334.         first->tree=0;
  1335.         eval_constn(tree);
  1336.         tree->ntyp->flags=t->flags;
  1337.         insert_const(tree);
  1338.         first->val=tree->val;
  1339.         free_expression(tree2);
  1340.         killsp();
  1341.         return(first);
  1342.     }else{
  1343.     /*  auch anderes erlaubt    */
  1344.         first=(struct const_list *)mymalloc(CLS);
  1345.         first->next=first->other=0;
  1346.         first->tree=tree;
  1347.         killsp();
  1348.         return(first);
  1349.     }
  1350. }
  1351.  
  1352.  
  1353.  
  1354.